package design.onekey.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import design.onekey.model.dto.UserInfoDto;
import design.onekey.mapper.SysMenuMapper;
import design.onekey.model.dto.IndexMenuTreeDto;
import design.onekey.model.SysMenu;
import design.onekey.model.SysMenuDto;
import design.onekey.service.ISysMenuService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @Description: SysMenu服务实现
* @author
* @date 2020-06-05 04:03:09
* @version V1.0
*/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {



   /**
    * @Title: getIndexMenu
    * @Description: 获取首页菜单树
    * @param userInfoDto
    * @return
    * @date 2020-06-11 05:21:25
    */
   @Override
   public List<IndexMenuTreeDto> getIndexMenu(UserInfoDto userInfoDto) {
       SysMenuDto sysMenuDto = new SysMenuDto();
       if (1 == userInfoDto.getIsAdmin()) {
           //超级管理员
           sysMenuDto.setIsAdmin(1);
       }else {
           sysMenuDto.setRoleId(userInfoDto.getRoleId());
       }
       sysMenuDto.setParentMenuId(0L);
       List<IndexMenuTreeDto> list = this.baseMapper.getIndexMenu(sysMenuDto);
       this.getSubIndexMenu(list, userInfoDto);
       return list;
   }

   /**
    * @Title: getSubIndexMenu
    * @Description: 获取子菜单树
    * @param list
    * @param userInfoDto
    * @author blcloak
    * @date 2020-06-11 05:55:27
    */
   private void getSubIndexMenu(List<IndexMenuTreeDto> list,UserInfoDto userInfoDto)
   {
       if (list != null && list.size() > 0) {
           for (int i = 0; i < list.size(); i++) {
               SysMenuDto sysMenuDto = new SysMenuDto();
               if (1 == userInfoDto.getIsAdmin()) {
                   //超级管理员
                   sysMenuDto.setIsAdmin(1);
               }else {
                   sysMenuDto.setRoleId(userInfoDto.getRoleId());
               }
               sysMenuDto.setParentMenuId(list.get(i).getId());
               List<IndexMenuTreeDto> subMenus =  this.baseMapper.getIndexMenu(sysMenuDto);
               list.get(i).setSubs(subMenus);
               this.getSubIndexMenu(subMenus, userInfoDto);
           }
       }
   }

}
